package com.ymatou.iapi.trading.sellerquery.testcase;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.sellerquery.parameter.GetSellerConsumersBean;
import com.ymatou.iapi.trading.sellerquery.parameter.resp.GetSellerConsumersResp;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.constant.ECode;

/**
 * 获取卖家顾客ID接口 FreeMaker Template自动生成代码
 * 接口已经转接java
 */
@TestSuite
public class Ts_GetSellerConsumers {
	private static GetSellerConsumersBean getsellerconsumersBean;
	private static com.ymatou.iapi.trading.sellerquery.service.GetSellerConsumersCall getsellerconsumersCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取卖家顾客ID接口");
	}

	@Before
	public void caseUp() {
		getsellerconsumersBean = new GetSellerConsumersBean();
		getsellerconsumersCall = new com.ymatou.iapi.trading.sellerquery.service.GetSellerConsumersCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getsellerconsumersCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	private void verify(){
		GetSellerConsumersResp expectResponse = new GetSellerConsumersResp();
		ConstructExpectedResponse(expectResponse);
		GetSellerConsumersResp actualResponse = getsellerconsumersCall.getSellerConsumersResp();
		DataUtil.verify(expectResponse.getConsumersCount(), actualResponse.getConsumersCount(), "验证ConsumersCount");
		List<String> expConsumers = expectResponse.getConsumers();
		List<String> actConsumers = actualResponse.getConsumers();
		if(expConsumers == null){
			DataUtil.verify(null, actConsumers, "验证consumers=null");
		}else{
			for(String expect:expConsumers){
				DataUtil.verify(true, actConsumers.stream().anyMatch(x->x.equals(expect)), "验证Consumers中是否包含期望值: "+expect);
			}
		}
	}
	
	@SuppressWarnings("rawtypes")
	private void ConstructExpectedResponse(GetSellerConsumersResp response){
		DefaultWapper db = new DefaultWapper();
		int consumersCount = 0;
		List<String> consumers = null;
		if(DataUtil.Booleann.GetValueOrDefault(getsellerconsumersBean.getCountOnly())){
			List<Map> t = db.executeSql(getSellerConsumersCountSql(DataUtil.Integerr.GetValueOrDefault(getsellerconsumersBean.getSellerId()), 
					getsellerconsumersBean.getShangou(), 
					DataUtil.Booleann.GetValueOrDefault(getsellerconsumersBean.getTurnover()), 
					DataUtil.Booleann.GetValueOrDefault(getsellerconsumersBean.getExcludeBlacklistConsumers()), 
					getsellerconsumersBean.getBeginTime(), getsellerconsumersBean.getEndTime()));
			consumersCount = DataUtil.isNullOrEmpty(t)?0:(int)t.get(0).get("total");
		}else{
			List<Map> t = db.executeSql(getSellerConsumersListSql(DataUtil.Integerr.GetValueOrDefault(getsellerconsumersBean.getSellerId()),
					getsellerconsumersBean.getShangou(), 
					DataUtil.Booleann.GetValueOrDefault(getsellerconsumersBean.getTurnover()), 
					DataUtil.Booleann.GetValueOrDefault(getsellerconsumersBean.getExcludeBlacklistConsumers()), 
					getsellerconsumersBean.getBeginTime(), getsellerconsumersBean.getEndTime()));
			if(DataUtil.isNullOrEmpty(t)){
				consumersCount = 0;
			}else{
				consumersCount = t.size();
				consumers = t.stream().map(x->DataUtil.Stringg.getValueOrNull(x.get("iUserId"))).collect(Collectors.toList());
			}
		}
		response.setConsumers(consumers);
		response.setConsumersCount(consumersCount);
	}

	public static String getSellerConsumersCountSql(int sellerId, Boolean shangou, boolean turnover,
            boolean excludeBlacklistConsumers, String beginTime, String endTime){
		StringBuilder sb = new StringBuilder();
		sb.append("select count(distinct iUserId) as total from Ymt_Orders(nolock) o ");
		if(excludeBlacklistConsumers){
			sb.append(" left join Ymt_BlackListForSeller(nolock) bl on o.iBuyerId = bl.iSellerId and o.iUserId = bl.iBuyerId and ");
			sb.append(" bl.iAction = 0");
		}
		sb.append(" where o.iBuyerId = "+sellerId);
		if(shangou != null){
			sb.append(" and o.bShangouOrder = "+DataUtil.bool2Int(shangou));
		}
		if(turnover){
			sb.append(" and o.iTradingStatus = 4");
		}
		if(DataUtil.Stringg.isNullOrEmpty(beginTime) == false){
			sb.append(" and o.dAddTime >= '"+beginTime+"'");
		}
		if(DataUtil.Stringg.isNullOrEmpty(endTime) == false){
			sb.append(" and o.dAddTime < '"+endTime+"'");
		}
		if(excludeBlacklistConsumers){
			sb.append(" and bl.iSellerId is null");
		}
		return sb.toString();
	}
	
	public static String getSellerConsumersListSql(int sellerId, Boolean shangou, boolean turnover,
            boolean excludeBlacklistConsumers, String beginTime, String endTime){
		StringBuilder sb = new StringBuilder();
		sb.append("select distinct iUserId from Ymt_Orders(nolock) o ");
		if(excludeBlacklistConsumers){
			sb.append(" left join Ymt_BlackListForSeller(nolock) bl on o.iBuyerId = bl.iSellerId and o.iUserId = bl.iBuyerId and ");
			sb.append(" bl.iAction = 0");
		}
		sb.append(" where o.iBuyerId = "+sellerId);
		if(shangou != null){
			sb.append(" and o.bShangouOrder = "+DataUtil.bool2Int(shangou));
		}
		if(turnover){
			sb.append(" and o.iTradingStatus = 4");
		}
		if(DataUtil.Stringg.isNullOrEmpty(beginTime) == false){
			sb.append(" and o.dAddTime >= '"+beginTime+"'");
		}
		if(DataUtil.Stringg.isNullOrEmpty(endTime) == false){
			sb.append(" and o.dAddTime < '"+endTime+"'");
		}
		if(excludeBlacklistConsumers){
			sb.append(" and bl.iSellerId is null");
		}
		return sb.toString();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetSellerConsumers_001() {
		Logger.start(true, "获取卖家顾客ID成功,countOnly=true");
		try {
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			String beginTime = YMTDateUtil.getBeforeOrNextDay(-10,
					"yyyy-MM-dd'T'HH:mm:ss");
			String endTime = YMTDateUtil.getBeforeOrNextDay(1,
					"yyyy-MM-dd'T'HH:mm:ss");

			getsellerconsumersBean.setSellerId(sellerId);
			getsellerconsumersBean
					.setExcludeBlacklistConsumers(false);
			getsellerconsumersBean.setCountOnly(true);
			getsellerconsumersBean.setTurnover(true);
			getsellerconsumersBean.setShangou(false);
			getsellerconsumersBean.setBeginTime(beginTime);
			getsellerconsumersBean.setEndTime(endTime);
			String param = FormatUtil.beanToHttpString(getsellerconsumersBean,
					true, false);
			getsellerconsumersCall.setData(param);
			getsellerconsumersCall.callService();
			Logger.verifyEquals(ECode.SUCCESS,
					getsellerconsumersCall.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetSellerConsumers_002() {
		Logger.start(true, "获取卖家顾客ID成功,countOnly=false");
		try {
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			String beginTime = YMTDateUtil.getBeforeOrNextDay(-100,
					"yyyy-MM-dd'T'HH:mm:ss");
			String endTime = YMTDateUtil.getBeforeOrNextDay(1,
					"yyyy-MM-dd'T'HH:mm:ss");

			getsellerconsumersBean.setSellerId(sellerId);
			getsellerconsumersBean
					.setExcludeBlacklistConsumers(false);
			getsellerconsumersBean.setCountOnly(false);
			getsellerconsumersBean.setTurnover(true);
			getsellerconsumersBean.setShangou(false);
			getsellerconsumersBean.setBeginTime(beginTime);
			getsellerconsumersBean.setEndTime(endTime);
			String param = FormatUtil.beanToHttpString(getsellerconsumersBean,
					true, false);
			getsellerconsumersCall.setData(param);
			getsellerconsumersCall.callService();
			Logger.verifyEquals(ECode.SUCCESS,
					getsellerconsumersCall.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetSellerConsumers_003() {
		Logger.start(true,
				"获取卖家顾客ID成功,excludeBlacklistConsumers=true,countOnly=false");
		try {
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			String beginTime = YMTDateUtil.getBeforeOrNextDay(-10,
					"yyyy-MM-dd'T'HH:mm:ss");
			String endTime = YMTDateUtil.getBeforeOrNextDay(1,
					"yyyy-MM-dd'T'HH:mm:ss");
			getsellerconsumersBean.setSellerId(sellerId);
			getsellerconsumersBean
					.setExcludeBlacklistConsumers(true);
			getsellerconsumersBean.setCountOnly(false);
			getsellerconsumersBean.setTurnover(true);
			getsellerconsumersBean.setShangou(false);
			getsellerconsumersBean.setBeginTime(beginTime);
			getsellerconsumersBean.setEndTime(endTime);
			String param = FormatUtil.beanToHttpString(getsellerconsumersBean,
					true, false);
			getsellerconsumersCall.setData(param);
			getsellerconsumersCall.callService();
			Logger.verifyEquals(ECode.SUCCESS,
					getsellerconsumersCall.getCode(), "验证code");
			Logger.verifyEquals(0, getsellerconsumersCall.getBCode(), "验证bcode");
			Logger.verifyEquals(true, getsellerconsumersCall.getData()
					.getBoolean("success"), "验证success");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetSellerConsumers_004() {
		Logger.start(true,
				"获取卖家顾客ID成功,excludeBlacklistConsumers=true,countOnly=true");
		try {
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			String beginTime = YMTDateUtil.getBeforeOrNextDay(-10,
					"yyyy-MM-dd'T'HH:mm:ss");
			String endTime = YMTDateUtil.getBeforeOrNextDay(1,
					"yyyy-MM-dd'T'HH:mm:ss");

			getsellerconsumersBean.setSellerId(sellerId);
			getsellerconsumersBean
					.setExcludeBlacklistConsumers(true);
			getsellerconsumersBean.setCountOnly(true);
			getsellerconsumersBean.setTurnover(true);
			getsellerconsumersBean.setShangou(false);
			getsellerconsumersBean.setBeginTime(beginTime);
			getsellerconsumersBean.setEndTime(endTime);
			String param = FormatUtil.beanToHttpString(getsellerconsumersBean,
					true, false);
			getsellerconsumersCall.setData(param);
			getsellerconsumersCall.callService();
			Logger.verifyEquals(ECode.SUCCESS,
					getsellerconsumersCall.getCode(), "验证code");
			Logger.verifyEquals(0, getsellerconsumersCall.getBCode(), "验证bcode");
			Logger.verifyEquals(true, getsellerconsumersCall.getData()
					.getBoolean("success"), "验证success");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetSellerConsumers_005() {
		Logger.start(true,
				"获取卖家顾客ID成功,excludeBlacklistConsumers=true,turnover=false");
		try {
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			String beginTime = YMTDateUtil.getBeforeOrNextDay(-10,
					"yyyy-MM-dd'T'HH:mm:ss");
			String endTime = YMTDateUtil.getBeforeOrNextDay(1,
					"yyyy-MM-dd'T'HH:mm:ss");

			getsellerconsumersBean.setSellerId(sellerId);
			getsellerconsumersBean
					.setExcludeBlacklistConsumers(true);
			getsellerconsumersBean.setCountOnly(true);
			getsellerconsumersBean.setTurnover(false);
			getsellerconsumersBean.setShangou(false);
			getsellerconsumersBean.setBeginTime(beginTime);
			getsellerconsumersBean.setEndTime(endTime);
			String param = FormatUtil.beanToHttpString(getsellerconsumersBean,
					true, false);
			getsellerconsumersCall.setData(param);
			getsellerconsumersCall.callService();
			Logger.verifyEquals(ECode.SUCCESS,
					getsellerconsumersCall.getCode(), "验证code");
			Logger.verifyEquals(0, getsellerconsumersCall.getBCode(), "验证bcode");
			Logger.verifyEquals(true, getsellerconsumersCall.getData()
					.getBoolean("success"), "验证success");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetSellerConsumers_006() {
		Logger.start(true,
				"获取卖家顾客ID成功,excludeBlacklistConsumers=true,shangou=true");
		try {
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			String beginTime = YMTDateUtil.getBeforeOrNextDay(-10,
					"yyyy-MM-dd'T'HH:mm:ss");
			String endTime = YMTDateUtil.getBeforeOrNextDay(1,
					"yyyy-MM-dd'T'HH:mm:ss");

			getsellerconsumersBean.setSellerId(sellerId);
			getsellerconsumersBean
					.setExcludeBlacklistConsumers(true);
			getsellerconsumersBean.setCountOnly(true);
			getsellerconsumersBean.setTurnover(false);
			getsellerconsumersBean.setShangou(false);
			getsellerconsumersBean.setBeginTime(beginTime);
			getsellerconsumersBean.setEndTime(endTime);
			String param = FormatUtil.beanToHttpString(getsellerconsumersBean,
					true, false);
			getsellerconsumersCall.setData(param);
			getsellerconsumersCall.callService();
			Logger.verifyEquals(ECode.SUCCESS,
					getsellerconsumersCall.getCode(), "验证code");
			Logger.verifyEquals(0, getsellerconsumersCall.getBCode(), "验证bcode");
			Logger.verifyEquals(true, getsellerconsumersCall.getData()
					.getBoolean("success"), "验证success");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_GetSellerConsumers_007() {
		Logger.start(true,
				"获取卖家顾客ID成功,excludeBlacklistConsumers=true,shangou 为null");
		try {
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			String beginTime = YMTDateUtil.getBeforeOrNextDay(-10,
					"yyyy-MM-dd'T'HH:mm:ss");
			String endTime = YMTDateUtil.getBeforeOrNextDay(1,
					"yyyy-MM-dd'T'HH:mm:ss");

			getsellerconsumersBean.setSellerId(sellerId);
			getsellerconsumersBean
					.setExcludeBlacklistConsumers(true);
			getsellerconsumersBean.setCountOnly(true);
			getsellerconsumersBean.setTurnover(false);
			getsellerconsumersBean.setBeginTime(beginTime);
			getsellerconsumersBean.setEndTime(endTime);
			String param = FormatUtil.beanToHttpString(getsellerconsumersBean,
					true, false);
			getsellerconsumersCall.setData(param);
			getsellerconsumersCall.callService();
			Logger.verifyEquals(ECode.SUCCESS,
					getsellerconsumersCall.getCode(), "验证code");
			Logger.verifyEquals(0, getsellerconsumersCall.getBCode(), "验证bcode");
			Logger.verifyEquals(true, getsellerconsumersCall.getData()
					.getBoolean("success"), "验证success");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_GetSellerConsumers_008() {
		Logger.start(true,
				"获取卖家顾客ID成功,excludeBlacklistConsumers=true,shangou 为null,turnover 为null");
		try {
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			String beginTime = YMTDateUtil.getBeforeOrNextDay(-10,
					"yyyy-MM-dd'T'HH:mm:ss");
			String endTime = YMTDateUtil.getBeforeOrNextDay(1,
					"yyyy-MM-dd'T'HH:mm:ss");

			getsellerconsumersBean.setSellerId(sellerId);
			getsellerconsumersBean
					.setExcludeBlacklistConsumers(true);
			getsellerconsumersBean.setCountOnly(true);
			getsellerconsumersBean.setBeginTime(beginTime);
			getsellerconsumersBean.setEndTime(endTime);
			String param = FormatUtil.beanToHttpString(getsellerconsumersBean,
					true, false);
			getsellerconsumersCall.setData(param);
			getsellerconsumersCall.callService();
			Logger.verifyEquals(ECode.SUCCESS,
					getsellerconsumersCall.getCode(), "验证code");
			Logger.verifyEquals(0, getsellerconsumersCall.getBCode(), "验证bcode");
			Logger.verifyEquals(true, getsellerconsumersCall.getData()
					.getBoolean("success"), "验证success");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_GetSellerConsumers_009() {
		Logger.start(true, "sellerid 不存在");
		try {
			String beginTime = YMTDateUtil.getBeforeOrNextDay(-10,
					"yyyy-MM-dd'T'HH:mm:ss");
			String endTime = YMTDateUtil.getBeforeOrNextDay(1,
					"yyyy-MM-dd'T'HH:mm:ss");

			getsellerconsumersBean.setSellerId(404);
			getsellerconsumersBean
					.setExcludeBlacklistConsumers(true);
			getsellerconsumersBean.setCountOnly(true);
			getsellerconsumersBean.setBeginTime(beginTime);
			getsellerconsumersBean.setEndTime(endTime);
			String param = FormatUtil.beanToHttpString(getsellerconsumersBean,
					true, false);
			getsellerconsumersCall.setData(param);
			getsellerconsumersCall.callService();
			Logger.verifyEquals(ECode.SUCCESS,
					getsellerconsumersCall.getCode(), "验证code");
			Logger.verifyEquals(0, getsellerconsumersCall.getBCode(), "验证bcode");
			Logger.verifyEquals(true, getsellerconsumersCall.getData()
					.getBoolean("success"), "验证success");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_GetSellerConsumers_010() {
		Logger.start(true, "sellerid为null");
		try {
			String beginTime = YMTDateUtil.getBeforeOrNextDay(-10,
					"yyyy-MM-dd'T'HH:mm:ss");
			String endTime = YMTDateUtil.getBeforeOrNextDay(1,
					"yyyy-MM-dd'T'HH:mm:ss");
			getsellerconsumersBean
					.setExcludeBlacklistConsumers(true);
			getsellerconsumersBean.setCountOnly(true);
			getsellerconsumersBean.setBeginTime(beginTime);
			getsellerconsumersBean.setEndTime(endTime);
			String param = FormatUtil.beanToHttpString(getsellerconsumersBean,
					true, false);
			getsellerconsumersCall.setData(param);
			getsellerconsumersCall.callService();
			Logger.verifyEquals(ECode.ERROR400,
					getsellerconsumersCall.getCode(), "验证code");
			Logger.verifyEquals(100, getsellerconsumersCall.getBCode(),
					"验证bcode");
			Logger.verifyEquals("sellerId:sellerId不能为0",
					getsellerconsumersCall.getMsg(), "验证bcode");
			Logger.verifyEquals(false, getsellerconsumersCall.getData()
					.getBoolean("success"), "验证success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}